﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheatEngineTableTranslation
{
	public partial class Home : Form
	{
		string filePath = string.Empty;

		public Home()
		{
			InitializeComponent();
			ComboBox1InitializeComponent();
		}

		private string[] ComboBox1Items = { "下拉选择功能", "复制原文", "粘贴译文", "本地翻译", "保存翻译", "重置" };

		private void ComboBox1InitializeComponent() 
		{
			comboBox1.Items.AddRange(ComboBox1Items);
			comboBox1.SelectedIndex = 0;
		}

		private void Home_DragEnter(object sender, DragEventArgs e)
		{
			// 检查拖拽的内容是否是文件
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				// 获取拖拽的文件路径列表
				string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

				// 检查所有文件是否都以 .ct 结尾（忽略大小写）
				bool allFilesAreCt = files.All(file => Path.GetExtension(file).Equals(".ct", StringComparison.OrdinalIgnoreCase));

				if (allFilesAreCt)
				{
					e.Effect = DragDropEffects.Copy; // 允许拖拽操作
				}
				else
				{
					e.Effect = DragDropEffects.None; // 禁止拖拽操作
				}
			}
			else
			{
				e.Effect = DragDropEffects.None; // 禁止拖拽操作
			}
		}

		private void Home_DragDrop(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

				if (files.Length > 0)
				{
					filePath = files[0]; // 获取第一个文件路径
					textBox1.Text = FileOperate.ReadCTFile(filePath, richTextBox1);
					richTextBox1.Enabled = true;
					richTextBox1.Visible = true;
					comboBox1.Enabled = true;
					panel1.Visible = false;
					panel1.Enabled = false;
				}
			}
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (comboBox1.SelectedIndex==1) 
			{
				Clipboard.SetText(richTextBox1.Text);
				textBox1.Text = "已把原文复制到粘贴板上";
				return;
			}
			if (comboBox1.SelectedIndex == 2)
			{
				panel2.Visible = false;
				panel2.Enabled = false;
				richTextBox2.Enabled = true;
				richTextBox2.Visible = true;
				richTextBox2.Text = Clipboard.GetText();
				textBox1.Text = FileOperate.ReadTranslateByRTB(richTextBox2);
				textBox1.Text += FileOperate.SaveTranslations();
				return;
			}
			if (comboBox1.SelectedIndex == 3) 
			{
				string csvFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Translation.csv");

				if (!File.Exists(csvFilePath))
				{
					textBox1.Text = "发生错误: 翻译文件不存在！";
					return;
				}
				FileInfo fileInfo = new FileInfo(csvFilePath);
				if (fileInfo.Length == 0)
				{
					textBox1.Text = "发生错误: 翻译文件为空！";
					return;
				}
				richTextBox2.Enabled = true;
				richTextBox2.Visible = true;
				panel2.Visible = false;
				panel2.Enabled = false;
				textBox1.Text = FileOperate.LocalTranslation(richTextBox2);
				return;
			}
			if (comboBox1.SelectedIndex == 4) 
			{
				if (string.IsNullOrWhiteSpace(textBox1.Text) || textBox1.Text.Contains("警告"))
				{
					textBox1.Text = $"保存失败，原因：{textBox1.Text}";
					return;
				}

				if (!FileOperate.WriteCTFiles(filePath))
				{
					textBox1.Text = $"保存失败，原因：{FileOperate.ErrorByWriteCTFile}";
					return;
				}
				richTextBox2.Text = "";
				richTextBox1.Text = "";
				richTextBox1.Enabled = false;
				richTextBox1.Visible = false;
				richTextBox2.Enabled =false;
				richTextBox2.Visible =false;
				comboBox1.Enabled = false;
				panel1.Visible = true;
				panel2.Visible = true;
				panel1.Enabled = true;
				panel2.Enabled =true;
				textBox1.Text = "翻译成功！";
				return;
			}
			if (comboBox1.SelectedIndex == 5)
			{
				richTextBox2.Text = "";
				richTextBox1.Text = "";
				richTextBox1.Enabled = false;
				richTextBox1.Visible = false;
				richTextBox2.Enabled = false;
				richTextBox2.Visible = false;
				comboBox1.Enabled = false;
				panel1.Visible = true;
				panel2.Visible = true;
				panel1.Enabled = true;
				panel2.Enabled = true;
				textBox1.Text = "重置成功！";
				return;
			}
		}
	}
}
